import {SetDownloaderLib} from "../../../../../static/lib/downloader/SetDownloaderLib";

export class SetDownloader {
    constructor(downloadParam, successCallback, errorCallback, progressCallback) {
        this.successCallback = successCallback;
        this.errorCallback = errorCallback;
        this.progressCallback = progressCallback;
        this.downloaderId = downloadParam.SetDownloadId;
        downloadParam = JSON.stringify(downloadParam);
        this.downloadParam = downloadParam;
    }

    /**
     * 构造一组文件的下载器, 同时只能有一个下载器
     * @param downloadParam 下载的入参 eg:[{TaskId:'1240970803742253058',FileType:'PolyvVideo',Vid:'6625fbaf94dce464d6a5ee6be5b18f98_6',FileName:'1240970803742253058',Url:''},{TaskId:'1240176712221728770',FileType:'courseware',FileName:'1240176712221728770.ppt',Url:'https://content.kquestions.com/6db60be4-3910-425b-bea8-c5f98b854c73.ppt',Vid:''}];
     * @param successCallback 成功的回调
     * @param errorCallback 失败的回调
     * @param progressCallback 进度回调
     */
    static startSingleSetDownloader(downloadParam, successCallback, errorCallback, progressCallback) {
        let setDownloaderLib = new SetDownloaderLib(downloadParam, function () {
            if(successCallback){
                successCallback(JSON.stringify({ReturnValue: "success"}))
            }
            setDownloaderLib.startDownload(successCallback, errorCallback, progressCallback);
        });
    }

    /**
     * 销毁单个下载器，与初始化对应，必须人工销毁
     */
    static destorySingleSetDownloader() {
        if (window.setDownloader) {
            let downloaderId = window.setDownloader.downloaderId;
            if(downloaderId){
                cefCustomObject.cancelSetDownload(downloaderId);
            }
            window.setDownloader = null;
        }
    }

    /**
     * 塞入下载中心并启动
     * @param setDownloader
     */
    static pushDownloadCenter(setDownloader) {
        if (window.downloadCenter) {
            if (window.downloadCenter.has(setDownloader.downloaderId)) {
                cefCustomObject.cancelSetDownload(setDownloader.downloaderId);
                window.downloadCenter.delete(setDownloaderId);
            }
            window.downloadCenter.set(setDownloader.downloaderId, setDownloader);
        } else {
            window.downloadCenter = new Map();
            window.downloadCenter.set(setDownloader.downloaderId, setDownloader);
        }
        cefCustomObject.pushStartDownloader(setDownloader.downloadParam);
    }

    static removeFromDownloadCenter(setDownloaderId) {
        if (window.downloadCenter) {
            if (window.downloadCenter.has(setDownloaderId)) {
                cefCustomObject.cancelSetDownload(downloaderId);
                window.downloadCenter.delete(setDownloaderId);
            }
        }
    }

    success(jsonParam) {
        if (this.successCallback) {
            this.successCallback(jsonParam);
        }
    }

    fail(jsonParam) {
        if (this.errorCallback) {
            this.errorCallback(jsonParam);
        }
    }

    progress(jsonParam) {
        if (this.progressCallback) {
            this.progressCallback(jsonParam);
        }
    }
}
